// 封装分类数据相关代码

import { onMounted, ref } from "vue";
import { onBeforeRouteUpdate, useRoute } from "vue-router";
import {getCategoryAPI} from "@/apis/category"
export function useCategory() {
    // 获取数据
    const categoryData = ref({});
    const route = useRoute();
    const getCategory = async (id = route.params.id) => {
        const res = await getCategoryAPI(id);
        categoryData.value = res.result;
    };

    onMounted(() => {
        getCategory();
    });



    // 目标: 路由参数变化,可以重新发送请求
    onBeforeRouteUpdate((to) => {
        console.log("路由变化");
        getCategory(to.params.id);
    });

    return{
        categoryData
    }
}